package com.hqyj.second.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.second.domain.Users;
import com.hqyj.second.mapper.UsersMapper;
import com.hqyj.second.repository.UsersRepository;
import com.hqyj.second.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper,Users> implements UsersService {

    @Resource
    //@Autowired
    private UsersRepository usersRepository;

    @Override
    public Users saveUser(Users u) throws Exception {

        return usersRepository.save(u);
    }

    @Override
    public Users findUserByUid(Integer uid) {
        Optional<Users> optional = usersRepository.findById(uid);
        return optional.orElse(null);
    }

    @Override
    public List<Users> findAllUsers() {
        List<Users> usersList = usersRepository.findAll();
        return usersList;
    }

    @Override
    public Users updateUser(Users u) throws Exception {
        return usersRepository.save(u);
    }

    @Override
    public boolean deleteUser(Integer uid) throws Exception {
        try {
            usersRepository.deleteById(uid);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public boolean insertUser(Users u) throws Exception {
        return super.save(u);
    }

    @Override
    public Users queryUserByUid(Integer uid) {
        return super.getById(uid);
    }

    @Override
    public List<Users> queryAllUsers() {
        return super.list(null);
    }

    @Override
    public boolean modifyUser(Users u) throws Exception {
        return super.updateById(u);
    }

    @Override
    public boolean removeUser(Integer uid) throws Exception {
        return super.removeById(uid);
    }
}
